#pragma once

#include "../comm/log.hpp"
#include "../comm/util.hpp"
#include <cassert>
#include <vector>
#include <unordered_map>
// 根据题目list文件，加载所有的题目信息到内存中
// model:主要用来和数据进行交互，对外提供访问的接口

namespace ns_model
{
    using namespace std;
    using namespace ns_log;
    using namespace ns_util;

    struct Question
    {
        string number; // 题目唯一编号
        string title;  // 题目标题
        string star;   // 题目难度
        int cpu_limit; // 题目的时间要求(s)
        int mem_limit; // 题目的空间要求(KB)
        string desc;   // 题目的描述
        string header; // 题目预设给用户在线编译器的代码
        string tail;   // 题目的测试用例，与header拼接形成完整代码
    };

    const std::string question_list = "./questions/questions.list";
    const std::string question_path = "./questions/";

    class Model
    {
    public:
        Model()
        {
            assert(LoadQuestionList(question_list));
        }
        bool LoadQuestionList(const string &question_list)
        {
            ifstream in(question_list);
            if (!in.is_open())
            {
                lg(Fatal, "加载题库失败，请检查题库文件");
                return false;
            }
            std::string line;
            while (getline(in, line))
            {
                vector<string> tokens;
                StringUtil::SplitString(line, &tokens, " ");
                // 1 判断回文数 简单 1 30000
                if (tokens.size() != 5)
                {
                    lg(Warning,"加载部分题目失败，请检查文件格式");
                    continue;
                }
                Question q;
                q.number = tokens[0];
                q.title = tokens[1];
                q.star = tokens[2];
                q.cpu_limit = stoi(tokens[3]);
                q.mem_limit = stoi(tokens[4]);

                // 题目路径拼接
                string path = question_path;
                path += q.number;
                path += "/";

                FileUtil::ReadFile(path + "desc.txt", &q.desc, true);
                FileUtil::ReadFile(path + "header.cpp", &q.header, true);
                FileUtil::ReadFile(path + "tail.cpp", &q.tail, true);
                
                _questions.insert({q.number, q});
            }
            lg(Info,"加载题库成功！！！");
            in.close();
            return true;
        }
        bool GetAllQuestions(vector<Question> *out)
        {
            if (_questions.size() == 0)
            {
                lg(ns_log::Error,"用户获取题库失败");
                return false;
            }

            for (auto &q : _questions)
            {
                // first:string, second:Question
                out->push_back(q.second);
            }
            return true;
        }

        bool GetOneQuestion(const string &number, Question *out)
        {
            const unordered_map<string, Question>::const_iterator &iter = _questions.find(number);
            if (iter == _questions.end())
            {
                lg(ns_log::Error,"用户题目失败，题目编号：%s",number.c_str());
                return false;
            }
            *out = iter->second;
            return true;
        }

    private:
        // first为题目编号  second为题目结构体
        unordered_map<string, Question> _questions;
    };
}